<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="文章简介" prop="desc">
        <el-input v-model="form.desc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="文章标签" prop="tag">
        <el-select
          v-model="form.tag"
          multiple 
          placeholder="请选择文章标签">
          <el-option
            v-for="item in tagData"
            :key="item._id"
            :label="item.name"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章封面">
        <el-upload
          action="http://localhost:3000"
          class="avatar-uploader"
          :show-file-list="false"
          accept=".jpg,.jpeg,.png,.gif,.bmp,.pdf,.JPG,.JPEG,.PBG,.GIF,.BMP,.PDF"
          :disabled="!!imageUrl.length"
          :before-upload="beforeUpload">
          <div class="avatar-content" v-if="imageUrl" >
            <img :src="imageUrl" class="avatar">
            <span class="shadow-content">
              <i class="el-icon-zoom-in" @click="dialogVisible = true"></i>
              <i class="el-icon-delete" @click="coverDel"></i>
            </span>
          </div>
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章状态" required>
        <el-radio v-model="form.state" :label="true" border>发布</el-radio>
        <el-radio v-model="form.state" :label="false" border>待发布</el-radio>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <mavon-editor v-model="form.content"/>
      </el-form-item>
    </el-form>
    <el-button type="primary" class="release" @click="release">发布</el-button>

    <!-- 封面放大 -->
    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img width="100%" :src="imageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import CryptoJS from 'crypto-js'

export default {
  layout: 'admin',
  data () {
    return {
      dialogVisible: false,
      disabled: false,
      imageUrl: '',
      form: {
        title: '',
        desc: '',
        tag: [],
        cover: '',
        state: true,
        content: ''
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        desc: [{ required: true, message: '请输入文章简介', trigger: 'blur' }],
        content: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      },
      tagData: []
    }
  },
  methods: {
    // 图片选取与展示
    beforeUpload (file) {
      let self = this
      // 将图片file转为base64
      var reader = new FileReader();
      reader.onload = function(e) {
        self.imageUrl = e.target.result
        self.form.cover = e.target.result
      }
      reader.readAsDataURL(file)
    },
    // 删除图片
    coverDel () {
      this.imageUrl = ''
      this.form.cover = ''
    },
    // 发布文章
    release () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$http.post('/article/add', this.form).then(res => {
            if (res.code === 0) {
              this.$message.success(res.msg)
              this.$router.push({path: '/admin'})
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          return false;
        }
      });
    }
  },
  created () {
    this.$http.get('/tag/get').then(res => {
      if (res.code === 0) {
        this.tagData = res.data
      }
    })
  }
}
</script>

<style lang="scss">
  .release {
    margin-top: 50px;
    float: right;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar-content {
    width: 178px;
    height: 178px;
    position: relative;
    .shadow-content {
      display: none;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(0,0,0,.5);
      line-height: 178px;
      i {
        font-size: 20px;
        color: #ffffff;
        &:first-child{
          margin-right: 20px;
        }
      }
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
    &:hover {
      .shadow-content {
        display: inline-block;
      }
    }
  }
</style>
